<template>
  <div>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowKey="(record) => record.id"
        :rowSelection="options.rowSelection"
      >
        <template slot="operator" v-if="hasPerm('articlecategory:add')">
          <a-button @click="$refs.addForm.add()" icon="plus" type="primary" v-if="hasPerm('articlecategory:add')">新增分类</a-button>
          <x-down
            style="display:none"
            v-if="hasPerm('articlecategory:export')"
            ref="batchExport"
            @batchExport="batchExport"
          />
        </template>
        <span slot="IsShow" slot-scope="text, record">
          <span slot="IsShow" >
            <a-switch v-model="record.cRoomState" @change="changeStatus(record)" />
          </span>
        </span>
        <span slot="action" slot-scope="text, record">
          <a v-if="hasPerm('articlecategory:edit')" @click="$refs.editForm.edit(record)">编辑</a>
          <a-divider type="vertical" v-if="hasPerm('articlecategory:edit') & hasPerm('articlecategory:delete')"/>
          <a-popconfirm v-if="hasPerm('articlecategory:delete')" placement="topRight" title="确认删除？" @confirm="() => singleDelete(record)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </s-table>
      <add-form ref="addForm" @ok="handleOk" />
      <edit-form ref="editForm" @ok="handleOk" />
    </a-card>
  </div>
</template>

<script>
import { STable, XCard, XDown } from '@/components'
import { categoryPage, categoryDelete } from '@/api/modular/garden/category'
import addForm from './addForm'
import editForm from './editForm'

export default {
  components: {
    XDown,
    XCard,
    STable,
    addForm,
    editForm
  },

  data () {
    return {
      // 查询参数
      queryParam: {
        CategoryName: '',
      },
      // 表头
      columns: [
        {
          title: '分类名称',
          dataIndex: 'categoryName'
        },
        {
          title: '是否可见',
          dataIndex: 'isShow',
          scopedSlots: { customRender: 'IsShow' }
        },
        {
          title: '排序',
          dataIndex: 'sortValue',
        },
        {
          title: '描述',
          dataIndex: 'remark',
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return categoryPage(Object.assign(parameter, this.queryParam)).then((res) => {
          return res.data
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      }
    }
  },

  created () {
    if (this.hasPerm('articlecategory:edit') || this.hasPerm('articlecategory:delete')) {
      this.columns.push({
        title: '操作',
        width: '150px',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' }
      })
    }
  },

  methods: {
    /**
     * 单个删除
     */
    singleDelete (record) {
      const param = [record.id]
      this.sysPosDelete(param)
    },
    /**
     * 批量删除
     */
    batchDelete () {
      const paramIds = this.selectedRowKeys.map((d) => {
        return d;
      })
      this.sysPosDelete(paramIds)
    },
    /**
     * 删除
     */
    sysPosDelete (param) {
      categoryDelete(param).then((res) => {
        if (res.success) {
          this.$message.success('删除成功')
          this.$refs.table.clearRefreshSelected()
        } else {
          this.$message.error('删除失败：' + res.message)
        }
      }).catch((err) => {
        this.$message.error('删除错误：' + err.message)
      })
    },
    changeStatus (record) {
      console.log(record)
    },
    /**
     * 批量导出
     */
    batchExport () {
      // const paramIds = this.selectedRowKeys.map((d) => {
      //   return { 'id': d }
      // })
      // sysPosExport(paramIds).then((res) => {
      //   this.$refs.batchExport.downloadfile(res)
      // })
    },
    handleOk () {
      this.$refs.table.refresh()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    }
  }

}
</script>

<style lang="less">
.table-operator {
  margin-bottom: 18px;
}
button {
  margin-right: 8px;
}
</style>
